
var request = require('request')
var urlList = require('../config/urlList')
var flagg = urlList.length
function broadcast(req, url, directory) {
    var urlTemp = "http://"+url + directory
    var requestData = req.body
    request({
        url: urlTemp,
        method: "POST",
        json: true,
        headers: {
            "content-type": "application/json",
            "Authorization": "token " + req.headers.authorization
        },
        body: requestData
    }, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            if(response.body.type == "error"){
                return false
            }
            console.log(url+"响应正确")
            return true
        } else {
            flagg --;
            return false
        }
    });
}
function broadcastToAll(req, urlList, directory) {
    console.log(flagg)
    urlList.forEach(function (value, i) {
        broadcast(req, value, directory)
    });
    if(flagg >= (urlList.length/3)*2){
        console.log("区块链网络响应正确")
        return true
    }else{
        console.log(flagg)
        console.log((urlList.length/3)*2)
        console.log("区块链网络响应错误")
        return false
    }

}
module.exports = {
    broadcastToAll
}